var express = require('express')
var router = express.Router()
var db = require('../db/index')

router.get('/', (req, res) => {
    let userInfo = req.session.userInfo
    res.render('index',{userInfo})
})

router.get('/test', (req, res) => {
    res.render('test')
})
router.get('/login', (req, res) => {
    res.render('login')
})

router.post('/login', (req, res) => {
    console.log(req.body)
    db.login(req.body.name, req.body.password, function (err, result) {
        if (err) {
            let msg = err
            console.log(msg)
            res.render('msg', { msg })
        } else {
            req.session.userInfo = result
            res.redirect('/users')
        }
    })
})

router.get("/logout", function (req, res) {
    req.session.userInfo = null
    res.redirect("/")
})

module.exports = router